package com.zatech.cgnci.project.controller.claim;

import com.alibaba.fastjson.JSON;
import com.zatech.cgnci.project.dto.*;
import com.zatech.cgnci.project.service.CgnciClaimCaseTaskService;
import com.zatech.cgnci.project.service.CgnciClaimSettlementTaskService;
import com.zatech.cgnci.project.vo.ApprovalConfigVO;
import com.zatech.cgnci.project.vo.CgnciClaimSettlementResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/api/claim/settlementTask")
@RequiredArgsConstructor
@Api(produces = "application/json", tags = "理赔结算管理api接口文档")
@Slf4j
public class CgnciClaimSettlementTaskController {

    @Autowired
    private CgnciClaimSettlementTaskService claimSettlementTaskService;
    @Autowired
    private CgnciClaimCaseTaskService cgnciClaimCaseTaskService;

    @PostMapping("/createSavePrepay")
    @ApiOperation(value = "预付申请-创建或更新保存")
    @Transactional(rollbackFor = Exception.class)
    public CgnciClaimSettlementResultVO createSavePrepay(@RequestBody @Valid CgnciClaimSettlementDTO req) {
        log.info("{}预付申请创建保存:{}", req.getCaseNo(), JSON.toJSONString(req));
        return claimSettlementTaskService.createSavePrepay(req);
    }

    @PostMapping("/createSaveSettlement")
    @ApiOperation(value = "结算申请-创建或更新保存")
    @Transactional(rollbackFor = Exception.class)
    public CgnciClaimSettlementResultVO createSaveSettlement(@RequestBody @Valid CgnciClaimSettlementDTO req) {
        log.info("{}结算申请创建保存:{}", req.getCaseNo(), JSON.toJSONString(req));
        return claimSettlementTaskService.createSaveSettlement(req);
    }

    @PostMapping("/submitCheck")
    @ApiOperation(value = "提交校验")
    public Boolean submitCheck(@RequestBody SettlementQueryDTO req) {
        return claimSettlementTaskService.submitCheck(req);
    }

    @PostMapping("/submit")
    @ApiOperation(value = "提交")
    @Transactional(rollbackFor = Exception.class)
    public CgnciClaimSettlementResultVO submit(@RequestBody CgnciClaimSettlementSubmitDTO req) {
        Assert.hasText(req.getCaseNo(), "立案号必填.");
        Assert.notNull(req.getConfirm(), "是否确认必填.");
        Assert.notNull(req.getTaskType(), "任务类型必填.");
        log.info("{}预付/结算提交:{}", req.getCaseNo(), JSON.toJSONString(req));
        return claimSettlementTaskService.submitSettlement(req);
    }

    @GetMapping("/createCheck")
    @ApiOperation(value = "创建预付/结案校验")
    public void createCheck(@RequestParam("caseNo") String caseNo, @RequestParam("settlementType") String settlementType) {
        claimSettlementTaskService.createCheck(caseNo, settlementType);
    }

    @GetMapping("/delSettlement")
    @ApiOperation(value = "删除结算")
    @Transactional(rollbackFor = Exception.class)
    public void delSettlement(@RequestParam("settlementId") Long settlementId) {
        claimSettlementTaskService.delSettlement(settlementId);
    }


    @GetMapping("/delSettlementReinsurer")
    @ApiOperation(value = "删除结算再保人")
    @Transactional(rollbackFor = Exception.class)
    public void delSettlementReinsurer(@RequestParam(value = "reinsurerId") @NotNull(message = "reinsurerId不能为空") Long reinsurerId,
                                       @RequestParam(value = "sourceType") @NotNull(message = "sourceType不能为空") @ApiParam(value = "删除来源(1-我的案件,2-我的任务)") String sourceType) {
        claimSettlementTaskService.delSettlementReinsurer(reinsurerId, sourceType);
    }

    @PostMapping("/delSettlementDetail")
    @ApiOperation(value = "删除结算明细列表")
    @Transactional(rollbackFor = Exception.class)
    public void delSettlementDetail(@RequestBody CgnciClaimDelDTO req) {
        Assert.notEmpty(req.getDetailIdList(), "明细id缺失.");
        claimSettlementTaskService.delSettlementDetail(req);
    }

    @GetMapping("/logOffSettlement")
    @ApiOperation(value = "结算注销")
    @Transactional(rollbackFor = Exception.class)
    public void logOffSettlement(@RequestParam("taskNo") String taskNo) {
        Assert.hasText(taskNo, "任务号必填.");
        claimSettlementTaskService.logOffCase(taskNo);
    }

    @PostMapping("/approveSettlement")
    @ApiOperation(value = "审核同意")
    @Transactional(rollbackFor = Exception.class)
    public void approveSettlement(@RequestBody CgnciClaimCaseCheckDTO req) {
        Assert.hasText(req.getTaskNo(), "任务号必填.");
        log.info("{}审核同意approveSettlement:{}", req.getTaskNo(), JSON.toJSONString(req));
        claimSettlementTaskService.approveSettlement(req);
    }

    @GetMapping("/listBackNode")
    @ApiOperation(value = "获取退回流程节点")
    @Transactional(rollbackFor = Exception.class)
    public List<ApprovalConfigVO> listBackNode(@RequestParam(value = "taskNo") String taskNo) {
        return claimSettlementTaskService.listBackNode(taskNo);
    }

    @PostMapping("/backSettlement")
    @ApiOperation(value = "退回")
    @Transactional(rollbackFor = Exception.class)
    public void backSettlement(@RequestBody CgnciClaimCaseCheckDTO req) {
        Assert.hasText(req.getTaskNo(), "案件任务号必填。");
        Assert.notNull(req.getApprovalNode(), "退回节点必填。");
        log.info("{}退回backSettlement:{}", req.getTaskNo(), JSON.toJSONString(req));
        claimSettlementTaskService.backSettlement(req);
    }
}
